package controller.command {
	import com.facebook.graph.Facebook;
	import com.facebook.graph.FacebookDesktop;
	
	import model.ConfigModel;
	import model.UserModel;
	
	import org.robotlegs.mvcs.Command;
	
	public class FBLoginCommand extends Command {
		
		[Inject] public var configModel:ConfigModel;
		[Inject] public var userModel:UserModel;
		
		public override function execute():void {
			
			commandMap.detain(this);
			FacebookDesktop.init(configModel.fbAppId, loginHandler);	
		} 
		
		private function loginHandler(success:Object, fail:Object):void {
			if(fail) {
				FacebookDesktop.login(loginHandler, ["user_photo_video_tags" , "friends_photo_video_tags", "user_photos", "friends_photos"]);
			}else {
				
				userModel.firstName = success.user.first_name;
				userModel.lastName = success.user.last_name;
				
				commandMap.release(this);
				commandMap.execute(FBGetUserPhotos);
			}
		}
	}
}